<template>
  <div class="layout-container">
    <el-container>
      <el-header>
        <div class="header-left">
          <h2>购药管理系统</h2>
        </div>
        <div class="header-center">
          <el-menu mode="horizontal" :router="true" :default-active="activeMenu">
            <el-menu-item index="/user/home">
              <el-icon><House /></el-icon>首页
            </el-menu-item>
            <el-menu-item index="/user/cart">
              <el-icon><ShoppingCart /></el-icon>购物车
            </el-menu-item>
            <el-menu-item index="/user/orders">
              <el-icon><List /></el-icon>我的订单
            </el-menu-item>
            <el-menu-item index="/user/announcements">
              <el-icon><Bell /></el-icon>系统公告
            </el-menu-item>
          </el-menu>
        </div>
        <div class="header-right">
          <el-dropdown trigger="click">
            <span class="user-info">
              {{ userStore.user.name }}
              <el-icon><CaretBottom /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="$router.push('/user/profile')">
                  <el-icon><User /></el-icon>个人中心
                </el-dropdown-item>
                <el-dropdown-item @click="handleLogout">
                  <el-icon><SwitchButton /></el-icon>退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../stores/user'
import { 
  House, 
  ShoppingCart, 
  List, 
  Bell, 
  CaretBottom, 
  User, 
  SwitchButton 
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const activeMenu = ref(route.path)

const handleLogout = async () => {
  userStore.logout()
  await router.push('/login')
  ElMessage.success('退出成功')
}

onMounted(() => {
  if (!userStore.isAuthenticated) {
    router.push('/login')
  }
})
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-container {
  height: 100%;
}

.el-header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left h2 {
  margin: 0;
  color: #409EFF;
}

.header-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}

:deep(.el-menu) {
  border-bottom: none;
}

:deep(.el-menu-item) {
  display: flex;
  align-items: center;
  gap: 4px;
}
</style> 